#include "spellcheck.h"
#include <fstream>  // ifstream.
#include <iostream>
#include <string>
#include <cctype>  // tolower.
#include <algorithm>  // transform.
#include <sstream>  // istringstream.

char lowercase(char letter)
{
    return std::tolower(letter);
}

namespace spellcheck {
    Dictionary::Dictionary(std::ifstream& dict_file)
    {
        std::string dict_word;


        // Probably a way to use the return value of operator>> instead of
        // doing explicit state check with good().
        while (dict_file.good()) {
            dict_file >> dict_word;
            std::transform(dict_word.begin(), dict_word.end(), dict_word.begin(),
                    lowercase);
            word_set.insert(dict_word);
        }
    }

    bool Dictionary::check(const std::string& word)
    {
	std::string lowercase_copy(word);

	std::transform(lowercase_copy.begin(), lowercase_copy.end(),
			lowercase_copy.begin(), lowercase);

        return (word_set.find(lowercase_copy) != word_set.end());
    }
};

int
main(int argc, char *argv[])
{
    std::ifstream dict_file(argv[1]);
    std::istringstream unparsed_stream(argv[2]);
    spellcheck::Dictionary dictionary(dict_file);

    while (unparsed_stream.good()) {
        std::string to_check;

        unparsed_stream >> to_check;

        if (!dictionary.check(to_check)) {
            std::cout << to_check << std::endl;
        }
    }
}
